package cn.migu.music.hangupsms.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.migu.music.cljz.common.ReturnCode;
import cn.migu.music.cljz.common.dataobject.BaseRsp;
import cn.migu.music.common.utils.LogUtils;
import cn.migu.music.hangupsms.common.config.HangUpSmsConfig;
import cn.migu.music.hangupsms.remote.CeLueShiFeignClient;
import cn.migu.music.hangupsms.remote.model.CeLueShiResponse;
import cn.migu.music.hangupsms.remote.model.CeLueShiSms;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class CeLueShiService {
    private static final String CE_LUE_SHI_FLAG = "策略师短信";
    public static final String  SECOND_FORMAT   = "yyyyMMddHHmmss";
    @Autowired
    private HangUpSmsConfig     hangUpSmsConfig;
    @Autowired
    private CeLueShiFeignClient ceLueShiFeignClient;

    public SmsSendResult consumedByCeLueShi(String msg, String provinceId, String phoneNumber, String msgTemplateId, Long smsSendEndTime) {
        BaseRsp baseRsp = new BaseRsp();
        SmsSendResult smsSendResult = new SmsSendResult();
        boolean ceLueShiTemplate = false;
        smsSendResult.setMsg(msg);
        smsSendResult.setConsumed(false);
        if (StringUtils.startsWith(msg, CE_LUE_SHI_FLAG)) {
            List<String> msgList = getCeLueShiMsg(msg);
            smsSendResult.setActivityId(msgList.get(0));
            smsSendResult.setMsg(msgList.get(1));
            ceLueShiTemplate = true;
        }
        if (!ceLueShiTemplate) {
            return smsSendResult;
        }
        int r = ThreadLocalRandom.current().nextInt(1, 101);
        if (r <= hangUpSmsConfig.getCeLueShiSendRatio()) {
            String activeId = smsSendResult.getActivityId();
            String sms = smsSendResult.getMsg();
            CeLueShiSms ceLueShiSms = new CeLueShiSms();
            ceLueShiSms.setActivityId(activeId);
            ceLueShiSms.setMsgContent(sms);
            ceLueShiSms.setProvinceId(provinceId);
            ceLueShiSms.setMsgtemplateId(msgTemplateId);
            ceLueShiSms.setSendEndTime(smsSendEndTime);
            ceLueShiSms.setMsisdn(phoneNumber);
            try {
                CeLueShiResponse rsp = ceLueShiFeignClient.sendSms(ceLueShiSms);
                baseRsp.setInfo(rsp.getMessage());
                baseRsp.setCode(StringUtils.equals("0000", rsp.getCode()) ? ReturnCode.success.getCode() : ReturnCode.systemIsBusy.getCode());
            } catch (Exception e) {
                LogUtils.error(e, log, "调用策略师异常");
                baseRsp.setCode(ReturnCode.unkownError.getCode());
                baseRsp.setInfo(e.getMessage());
            }
            smsSendResult.setConsumed(true);
            return smsSendResult;
        }
        return smsSendResult;
    }

    private List<String> getCeLueShiMsg(String msg) {
        List<String> msgList = new ArrayList<>();
        int len = StringUtils.length(CE_LUE_SHI_FLAG);
        int endPos = StringUtils.indexOf(msg, '#', len + 1);
        String activeId = StringUtils.substring(msg, len + 1, endPos);
        msgList.add(activeId);
        String sms = StringUtils.substring(msg, endPos + 1);
        msgList.add(sms);
        return msgList;
    }

    @Data
    public static class SmsSendResult {
        private BaseRsp baseRsp;
        private String  activityId;
        private String  msg;
        private Boolean consumed;
    }
}
